<?php

/**
 * Ignus Framework
 *
 * @author Sergey Palyanov <sergey.palyanov@gmail.com>
 * @link http://code.google.com/p/ignus-php/
 * @license http://apache.org/licenses/LICENSE-2.0
 * @package Ignus
 * @subpackage App
 * @since 0.7.4
 */

namespace Ignus;

/**
 * @package Ignus
 * @subpackage App
 * @since 0.7.4
 */
abstract class Http
{
    /**
      * Parses Accept-Language HTTP header value.
      * Returns indexed array of accepted languages from most preferred to less preferred.
      * May return empty array. Also, one of language may match an asterisk (*) which means 'any language'.
      * @static
      * @param string $header Accept-Language HTTP header value
      * @return array
      */
    public static function getLanguages($header = null)
    {
        if ($header == null) {
            if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
                return array();
            $header = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        }

        $languages = explode(',', strtolower($header));

        foreach ($languages as &$lang) {
            $semicolon = strpos($lang, ';');
            if ($semicolon > 0)
                $lang = substr($lang, 0, $semicolon);
        }

        return $languages;
    }

    /**
     * Returns true if current request is an AJAX request
     * @return boolean
     */
    public static function isAjax()
    {
        if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']))
            return false;

        return strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
    }

    /**
     * Returns Site host name
     * @return string
     */
    public static function getHostname()
    {
        return isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '127.0.0.1';
    }

    /**
     * Returns client IP address
     * @return string|null
     */
    public static function getClientIp()
    {
        static $vars = array(
            'HTTP_CLIENT_IP',
            'HTTP_X_FORWARDED_FOR',
            'REMOTE_ADDR'
        );

        foreach ($vars as $var) {
            if (isset($_SERVER[$var]))
                return $_SERVER[$var];
        }

        return null;
    }

    /**
     * Returns server URL
     * @return string
     */
    public static function getUrl()
    {
        $scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
        return $scheme . '://' . self::getHostname();
    }

    /**
     * Return cookie value of default value if cookie is not set
     * @param string $name Cookie name
     * @param null $default Default value
     * @return string|null
     */
    public static function getCookie($name, $default = null)
    {
        return !empty($_COOKIE[$name]) ? $_COOKIE[$name] : $default;
    }

    /**
     * Sets cookie
     * @param string $name
     * @param mixed $value
     * @param integer $expire
     * @return boolean
     */
    public static function setCookie($name, $value, $expire = null)
    {
        if (is_string($expire))
            $expire = strtotime($expire);

        return setcookie($name, $value, $expire, '/');
    }

}
